#include "menu_main.h"
#include "message_dialog.h"

#include "bmp_sdcard.h"
#include "bmp_battery.h"
#include "bmp_light.h"
#include "bmp_speeker.h"

/**
 * for every window and widgets
 * set size first, then set childs' size and location
 */
MainMenu::MainMenu(HWND parent):Menu(parent)
{
    name ="MainMenu";

    setSize(212, 64);
    setPosition(0, 0);
    
    setupWidgets();
    setupGeometry();
}

MainMenu::~MainMenu()
{
    cleanWidgets();
}

void MainMenu::setupWidgets()
{
    pButton1 =new Button(this, "O  K");
    pButton2 =new Button(this, "CANCEL");
    pEditName =new Edit(this, "myname:");
    
    pBitmapButton1 =new BitmapButton(this, (Bitmap *)&bitmapContrast);
    pBitmapButton2 =new BitmapButton(this, (Bitmap *)&bitmapLight);
    pBitmapButton3 =new BitmapButton(this, (Bitmap *)&bitmapSpeekerOn);
    pBitmapButton4 =new BitmapButton(this, (Bitmap *)&bitmapSpeekerOff);
    pBitmapButton5 =new BitmapButton(this, (Bitmap *)&bitmapBattery9);
    pBitmapButton6 =new BitmapButton(this, (Bitmap *)&bitmapBattery9);

    //connect event messages
    //pButton1->signal[SIGNAL_BUTTON_UP].receiver =this;
    //pButton2->signal[SIGNAL_BUTTON_UP].receiver =this;
    //pBitmapButton1->signal[SIGNAL_BUTTON_UP].receiver =this;
}

void MainMenu::setupGeometry()
{
    pBitmapButton1->setPosition(1, 1);
    pBitmapButton2->setPosition(16, 1);
    pBitmapButton3->setPosition(31, 1);
    pBitmapButton4->setPosition(46, 1);
    pBitmapButton5->setPosition(61, 1);
    pBitmapButton6->setPosition(76, 1);

    pButton1->setPosition(10, 12);
    pButton2->setPosition(40, 12);
    pEditName->setPosition(10, 22);
}

void MainMenu::cleanWidgets()
{
    delete pBitmapButton1;
    delete pBitmapButton2;
    delete pBitmapButton3;
    delete pBitmapButton4;
    delete pBitmapButton5;
    delete pBitmapButton6;
    delete pEditName;
    delete pButton1;
    delete pButton2;
}

void MainMenu::draw(Rect *r)
{
    pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0);
    pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0xa);

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}

int MainMenu::button1Down()
{
    MessageDialog *pWarningMessage;
    
    pWarningMessage = new MessageDialog(this, "warning");
    pWarningMessage->setPosition(100,0);
    
    pWarningMessage->run();
    
    delete pWarningMessage;
    sendMessage(this, MSG_PAINT, 0,0);
    
    return 0;
}

int MainMenu::button2Down()
{
    asm("NOP\n");
    return 0;
}

int MainMenu::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
    case MSG_SIGNAL:
        if(SIGNAL_MATCH(wParam, &pButton1->signal[SIGNAL_BUTTON_UP]) )
        {
            button1Down();
        }
        
        if(SIGNAL_MATCH(wParam, &pButton2->signal[SIGNAL_BUTTON_UP]) )
        {
            button2Down();
        }
        if(SIGNAL_MATCH(wParam, &pBitmapButton1->signal[SIGNAL_BUTTON_UP]) )
        {
            button1Down();
        }
        
        break;
        
    }
    
    Menu::eventHandler(hWnd, iMsg, wParam, lParam);
    return 0;
}
